/**
 * TODO <br>
 * @author liuty
 * @since 2019年6月21日 下午4:01:23
 */
package com.irdstudio.efp.esb.service.impl.sed.pls;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.sed.pls.ReqQueryTellerInfoBean;
import com.irdstudio.efp.esb.service.bo.resp.pls.RespQueryTellerInfoBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.LhxESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.sed.pls.QueryTellerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
  * @author liuty
  * @since 2019年6月21日 下午4:01:23
  * @version 1.0
  */
@Service("queryTellerInfoService")
public class QueryTellerInfoServiceImpl implements QueryTellerInfoService {
	//日志记录器
	private static Logger logger = LoggerFactory.getLogger(QueryTellerInfoServiceImpl.class);
		
	@Override
	public RespQueryTellerInfoBean queryTellerInfo(ReqQueryTellerInfoBean reqQueryTellerInfoBean) throws Exception {
		logger.debug("正在调用柜员基本信息查询接口[50130001_01_MS2009]，请求参数：" + JSONObject.toJSONString(reqQueryTellerInfoBean));
		RespQueryTellerInfoBean respQueryTellerInfoBean = null;
		try {
			EsbRespServiceBean esbRespServiceBean = ESBClientFactory.buildClient()
					.withBody(reqQueryTellerInfoBean)
					.withTradeNo(ESBConstant.SYSTEM_ID_CBS_50130001)
					.withScene(ESBConstant.SCENE_NO_01)
					.withESBBeanFactory(LhxESBBeanCreator.getINSTANCE())
					.create()
					.sendAndReceive();
			if(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
				respQueryTellerInfoBean = esbRespServiceBean.getBody(RespQueryTellerInfoBean.class);
			} else {
				throw new ESBException("返回异常信息为:" + esbRespServiceBean.getSysHead().getRetInfArry().get(0).getRetMsg());
			}
			/*Optional.ofNullable(esbRespServiceBean.getSysHead()).orElseThrow(()->new ESBException("访问ESB发生异常，请检查请求参数是否正确或ESB是否连通"));
			List <EsbRespRetInfBean> retInfArry = esbRespServiceBean.getSysHead().getRetInfArry();
			
			if(!ESBConstant.SUCCESS.equals(retInfArry.get(0).getRetCd())) {
				throw new  ESBException("返回异常信息为:" + retInfArry.toString());
			}*/
			logger.info("调用柜员基本信息查询接口[50130001_01_MS2009]结束，返回信息：" + JSONObject.toJSONString(respQueryTellerInfoBean));
		} catch (ESBException e) {
			logger.error("调用柜员基本信息查询接口[50130001_01_MS2009]出现异常：" + e.getMessage());
			throw new ESBException("调用柜员基本信息查询接口[50130001_01_MS2009]出现异常：" + e.getMessage());
		} finally {
			logger.info("======>调用通用柜员基本信息查询接口[50130001_01_MS2009]结束<======");
		}
		return respQueryTellerInfoBean;
	}

}
